package com.chance.cc.crawler.development.scripts.car;

import com.alibaba.fastjson.JSONObject;
import com.chance.cc.crawler.core.CrawlerRecordContext;
import com.chance.cc.crawler.core.downloader.HttpPage;
import com.chance.cc.crawler.core.pipeline.result.CrawlerData;
import com.chance.cc.crawler.core.record.CrawlerRequestRecord;
import com.chance.cc.crawler.core.script.crawler.CrawlerCommonScript;
import com.chance.cc.crawler.core.selector.Selectable;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

import static com.chance.cc.crawler.core.CrawlerEnum.CrawlerDataType.*;

public class CarCrawlerScript extends CrawlerCommonScript {
   // private Logger log = (Logger) LoggerFactory.getLogger(CarCrawlerScript.class);
    private static final String DOMAIN = "car";
    private static final String CARURL = "https://www.dongchedi.com/auto/series/\\S*";//进入的主页面
    private static final String BRAND = "https://www.dongchedi.com/motor/pc/car/series/selected_series\\?city_name=\\?&series_ids=\\S*";//品牌页面
    private static final String FAQURL = "https://www.dongchedi.com/community/\\S*/wenda";//问答页面
    private static final String DETAILPAGEURL = "https://www.dongchedi.com/ugc/article/\\S*";//详细页面
    private static final String AUTHORURL = "https://www.dongchedi.com/user/\\S*";//作者页面

    private static final String CONNECTURL = "https://www.dongchedi.com";//页面链接拼接
    @Override
    public String domain() {
        return DOMAIN;
    }

    @Override
    public void initUrlRegulars() {
        addUrlRegular(CARURL);
        addUrlRegular(BRAND);
        addUrlRegular(FAQURL);
        addUrlRegular(DETAILPAGEURL);
        addUrlRegular(AUTHORURL);
    }
    @Override
    public boolean crawlerCheck(CrawlerRequestRecord crawlerRecord) {
        return true;
    }
    @Override
    public List<CrawlerRequestRecord> parseLinks(CrawlerRequestRecord crawlerRecord, HttpPage page) {
        List<CrawlerRequestRecord> parseLinks = new ArrayList<>();
        String url = crawlerRecord.getHttpRequest().getUrl();
        if(url.matches(CARURL)){//主页面解析
            this.entityPageParseLinks(crawlerRecord,page,parseLinks);
        }
        if(url.matches(FAQURL)){//问答页面解析
            this.faqPageParseLinks(crawlerRecord,page,parseLinks);
        }
        if(url.matches(DETAILPAGEURL)){//详情页面解析
            this.detailPageParseLinks(crawlerRecord,page,parseLinks);
        }
        return parseLinks;
    }
    //详细页面解析
    private void detailPageParseLinks(CrawlerRequestRecord crawlerRecord, HttpPage page, List<CrawlerRequestRecord> parseLinks) {
        //1.解析作者链接2.清洗下方评论
        String authorUrl = page.getHtml().xpath("//*[@id=\"__next\"]/div/div/div[2]/div[1]/div/div[1]/div/p/a/@href").get();
        authorUrl = CONNECTURL + authorUrl;
        CrawlerRequestRecord authorRecord = CrawlerRequestRecord.builder()
                .itemPageRequest(crawlerRecord)
                .httpUrl(authorUrl)
                .releaseTime(System.currentTimeMillis())
                .resultLabelTag(interaction)
                .copyBizTags()
                .build();
        parseLinks.add(authorRecord);
    }

    //问答页面解析
    private void faqPageParseLinks(CrawlerRequestRecord crawlerRecord, HttpPage page, List<CrawlerRequestRecord> parseLinks) {
        //获取页面的详细链接
        List<Selectable> all = page.getHtml().xpath("//*[@id=\"__next\"]/div[1]/div[2]/div/section/div[3]/div[1]").nodes();
        for (Selectable node : all){
            String detailUrl = node.xpath("./div[1]/div/a/@href").get();
            detailUrl = CONNECTURL + detailUrl;//得到详情页链接
            String articleDataId = "articleDataId";
            CrawlerRequestRecord detailRecord = CrawlerRequestRecord.builder()
                    .turnPageRequest(crawlerRecord)
                    .httpUrl(detailUrl)
                    .releaseTime(System.currentTimeMillis())
                    .resultLabelTag(article)
                    .copyBizTags()
                    .build();
            detailRecord.setNeedWashPage(true);
            detailRecord.tagsCreator().bizTags().addCustomKV("articleDataId",articleDataId);
            parseLinks.add(detailRecord);
        }
    }


    //主页面解析
    private void entityPageParseLinks(CrawlerRequestRecord crawlerRecord, HttpPage page, List<CrawlerRequestRecord> parseLinks) {
        //获取到品牌链接
        String brandUrl = crawlerRecord.tagsCreator().bizTags().getCategoryTag().getKVTagStrVal("brand");
        String objectDataId = "xuanyi";
        CrawlerRequestRecord brandRecord = CrawlerRequestRecord.builder()
                .itemPageRequest(crawlerRecord)
                .httpUrl(brandUrl)
                .releaseTime(System.currentTimeMillis())
                .resultLabelTag(article)
                .copyBizTags()
                .build();
        brandRecord.setNeedParsedPage(false);
        brandRecord.tagsCreator().bizTags().addCustomKV("objectDataId",objectDataId);
        parseLinks.add(brandRecord);
        //问答页面链接
        String faqUrl = page.getHtml().xpath("//*[@id=\"anchor-nav\"]/ul/li[6]/a/@href").get();
        faqUrl = CONNECTURL + faqUrl;
        CrawlerRequestRecord faqRecord = CrawlerRequestRecord.builder()
                .turnPageRequest(crawlerRecord)
                .httpUrl(faqUrl)
                .releaseTime(System.currentTimeMillis())
                .copyBizTags()
                .build();
        faqRecord.tagsCreator().bizTags().addCustomKV("objectDataId",objectDataId);
        parseLinks.add(faqRecord);
    }

    @Override
    public List<CrawlerData> washPage(CrawlerRequestRecord crawlerRecord, HttpPage page) {
        List<CrawlerData> crawlerData = new ArrayList<>();
        if(crawlerRecord.tagsCreator().resultTags().hasDataType(article)){
            crawlerData.add(washArticle(crawlerRecord,page));
        }
        if(crawlerRecord.tagsCreator().resultTags().hasDataType(interaction)){
            crawlerData.add(washInteraction(crawlerRecord,page));
        }
        if(crawlerRecord.tagsCreator().resultTags().hasDataType(comment)){
            crawlerData.add(washComment(crawlerRecord,page));
        }
        return crawlerData;
    }

    private CrawlerData washArticle(CrawlerRequestRecord crawlerRecord, HttpPage page) {
        String url = crawlerRecord.getHttpRequest().getUrl();
        CrawlerData crawlerData = null;
        if(url.matches(BRAND)){//清洗品牌页面
            crawlerData = this.washBrandPage(crawlerRecord,page);
        }
        if(url.matches(DETAILPAGEURL)){//清洗 详情页面
            crawlerData = this.washDetailPage(crawlerRecord,page);
        }
        return crawlerData;
    }
    //清洗详情页
    private CrawlerData washDetailPage(CrawlerRequestRecord crawlerRecord, HttpPage page) {
        String parentDataId = crawlerRecord.tagsCreator().bizTags().getCategoryTag().getKVTagStrVal("objectDataId");
        String artice = page.getHtml().xpath("//*[@id=\"__next\"]/div/div/div[2]/div[1]/div/div[2]/div[1]/div[2]/p//text()").get();
        String author = page.getHtml().xpath("//*[@id=\"__next\"]/div/div/div[2]/div[1]/div/div[1]/div/p/a//text()").get();
        String articleDataId = crawlerRecord.tagsCreator().bizTags().getCategoryTag().getKVTagStrVal("articleDataId");
       /* CrawlerData crawlerData = CrawlerData.builder()
                .data(crawlerRecord,page)
                .dataId(articleDataId)*/
        return null;
    }

    //清洗品牌页面
    private CrawlerData washBrandPage(CrawlerRequestRecord crawlerRecord, HttpPage page) {
        String list = page.getJson().jsonPath($_type+".data.list").get();
        JSONObject jsonObject = JSONObject.parseObject(list);
        String sub_brand_name = jsonObject.getString("sub_brand_name");//车系
        String series_name = jsonObject.getString("series_name");//车品牌
        String series_id = jsonObject.getString("motor_id");//车的id
        String objectDataId = crawlerRecord.tagsCreator().bizTags().getCategoryTag().getKVTagStrVal("objectDataId");
        CrawlerData crawlerData = CrawlerData.builder()
                .data(crawlerRecord,page)
                .dataId(StringUtils.joinWith("-",crawlerRecord.getDomain(),article.enumVal(),objectDataId))
                .resultLabelTag(article)
                .releaseTime(System.currentTimeMillis())
                .url(crawlerRecord.getHttpRequest().getUrl())
                .addContentKV("sub_brand_name",sub_brand_name)
                .addContentKV("series_name",series_name)
                .addContentKV("series_id",series_id)
                .build();
        return crawlerData;
    }

    private CrawlerData washComment(CrawlerRequestRecord crawlerRecord, HttpPage page) {
        return null;
    }

    private CrawlerData washInteraction(CrawlerRequestRecord crawlerRecord, HttpPage page) {
        return null;
    }

    //清洗时间
    private String changeTime(String dataTime) {

        return null;
    }


    @Override
    public void afterExecute(CrawlerRecordContext context) {

    }


}
